#!/bin/bash

# 拉取 Kafka 镜像
echo "拉取 wurstmeister/kafka docker 镜像..."
docker pull wurstmeister/kafka

# 读取 Zookeeper 连接地址
read -p "Enter the Zookeeper connect string (host:port): " zookeeper_connect

while true; do
    read -p "请输入kafka对外映射端口 (1-65535): " kafka_port
    if [[ ! $kafka_port =~ ^[0-9]+$ ]] || [ $kafka_port -le 0 ] || [ $kafka_port -gt 65535 ]; then
        echo "端口号无效。请输入一个在1到65535之间的数字."
    else
        break
    fi
done
# 确认 Kafka 监听地址
kafka_advertised_listeners="PLAINTEXT://$zookeeper_connect"

# 运行 Kafka 容器
echo "Starting Kafka container..."
docker run -d \
           --name kafka \
           -p $kafka_port:9092 \
           -e KAFKA_BROKER_ID=0 \
           -e KAFKA_ZOOKEEPER_CONNECT=$zookeeper_connect \
           -e KAFKA_ADVERTISED_LISTENERS=$kafka_advertised_listeners \
           -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 \
           -e KAFKA_HEAP_OPTS="-Xmx256M -Xms256M" \
           -e TZ="Asia/Shanghai" \
           wurstmeister/kafka

echo "Kafka container has been started."